<?php

namespace app\store\controller;

use library\Controller;
use think\Db;

/**
 * 商品分类管理
 * Class GoodsCate
 * @package app\store\controller
 */
class GoodsCate extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    protected $table = 'StoreGoods';
    protected $table_DB_Platform_Account_library = 'db_platform_accounts';            /*平台账户配置库*/
    protected $stat_remain = 'stat_remain';                                    /* 道具日志表 */
    protected $tradelog= 'tradelog';                                    /* 道具日志表 */
    protected $accounts_info= 'accounts_info';                                    /* 道具日志表 */
    protected function Db_connect_stat_remain(){
        $stat_remain= Db::connect($this->table_DB_Platform_Account_library)->table($this->stat_remain);
        return $stat_remain;
    }
    protected function Db_connect_tradelog(){
        $tradelog= Db::connect($this->table_DB_Platform_Account_library)->table($this->tradelog);
        return $tradelog;
    }
    protected function Db_connect_accounts_info(){
        $accounts_info= Db::connect($this->table_DB_Platform_Account_library)->table($this->accounts_info);
        return $accounts_info;
    }
    /**
     * 商品分类管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = 'LTV';
        $request = input('request.');
        $list =$this->Db_connect_stat_remain()
            ->where($this->structure_where())
            ->order('stat_time desc')
            ->paginate(22,false,['query'=>$request]);
        $data = $list->all();

        // 根据day获取具体的天数 默认查询注册当天数据
        $day_count = 1;
        if(!empty($request['day'])){
            $day_count = $request['day'];
        }

        foreach ($data as $k => $v) {
            if($v['channel'] == 888) {$v['channel_name']='全部渠道';}else {$v['channel_name']=get_channel_name($v['channel']);}
            $v['date'] = date('Y-m-d',strtotime($v['stat_time']));
            $endtime = date('Y-m-d H:i:s',strtotime($v['stat_time'])+24*3600);
            $map =[];
            if($v['channel'] != 888) {
                $map['channel_id']=$v['channel'];
            }

            // 查询总数据所用的时间
            $begin_time =$v['stat_time'];

            // 每次的循环ltv的次数不同 不能按照天数固定 应取（输入天数）和（当前时间与每天记录的日期相差天数）的最小值
            $differ_date=ceil((time()-strtotime($v['stat_time']))/86400);
            $my_count = min($day_count,$differ_date);

            for($day = 1;$day<=$my_count;$day++){
                // 每一天对前面所有天数进行累计
                $after_time =date('Y-m-d H:i:s',strtotime($v['stat_time'])+$day*24*3600);
                $amount = $this->Db_connect_tradelog()
                    ->where($map)
                    ->where('status',1)
                    ->where('registerat','>=',$v['stat_time'])
                    ->where('registerat','<=',$endtime)
                    ->where('createdat','>=',$begin_time)
                    ->where('createdat','<=',$after_time)
                    ->sum('amount');

                if($amount==0 || $v['dru']==0){
                    $v['day_amount'][$day]['ltv'] = $amount;
                }else{
                    $v['day_amount'][$day]['ltv']  =round($amount/$v['dru'],2);
                }

                $v['day_amount'][$day]['amount'] = $amount;

                //取最后一天的数据作为总的统计数据
                if($day == $day_count){
                    $v['amount']=$amount;
                }
            }

            $list[$k] = $v;
        }

        $data_arr= $list->toarray()['data'];
        $pages = $list->render();
        $start_time =date('Y-m-d',time()-7*24*3600).' - '.date('Y-m-d',time()-24*3600);
        /*渠道类型*/
        $channel_arr =Specific_parameters_Recharge_channel_arr();
        $this->assign('data_arr',$data_arr);
        $this->assign('channel_arr',$channel_arr);
        $this->assign('list',$list);
        $this->assign('start_time',$start_time);
        $this->assign('day_count',$day_count);
        $this->assign('pages', $pages);
        $this->fetch();
    }
    protected function structure_where(){
        $channel_type    = $this->request->param('channel_type');
        $search_time    = $this->request->param('search_time');
        $where=[];
        if(!empty($search_time)){
            $list = explode(' - ',$search_time);
            $begin =$list[0];
            $after =$list[1];
            $where[] =['stat_time','between',[$begin,$after]];
        }else{
            $begin =date('Y-m-d',time()-7*24*3600);
            $after =date('Y-m-d',time()-24*3600);
            $where[] =['stat_time','between',[$begin,$after]];
        }
        if(!empty($channel_type)){
            if($channel_type==30)$channel_type=0;
            $where[] =['channel','eq',$channel_type];
        }else{
            $where[] =['channel','eq',888];
        }
        return $where;
    }
    /**
     * 添加商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->title = '添加商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->title = '编辑商品分类';
        $this->_form($this->table, 'form');
    }

    /**
     * 禁用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    /**
     * 启用商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 删除商品分类
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
